<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;



use app\admin\model\ComplaintModel;

class Complaint extends Base
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                // $where[] = ['orde_o_code', 'like', '%' . $param['searchText'] . '%'];
                $where[] = ['type', '=', $param['searchText']];
            }
            $user = new ComplaintModel();
            $selectResult = $user->getDatasByWhere($where, $offset, $limit);

            // 拼装参数
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['user'] = showOperate([
                    $vo->memberModel->memb_phone => [
                        'auth' => 'member/edit',
                        'href' => "javascript:memberInfo(" .$vo['uid'] .")",
                        'btnStyle' => 'primary',
                        'icon' => 'fa fa-address-book'
                    ]
                ]);

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id'], $vo['status']));
            }
            $return['total'] = $user->getAllDatas($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }


    // 删除
    public function del()
    {
        $id = input('param.id');

        $role = new ComplaintModel();
        $flag = $role->delData($id);
        return json($flag);
    }

    public function deal($id){
        ComplaintModel::where('id', $id)->update(['status'=>1]);
        return json(msg(1, '', '已处理'));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id, $status)
    {
        $btn = [];
        if(!$status) {
            $btn['处理'] = [
                'auth' => 'complaint/deal',
                'href' => "javascript:deal(" .$id .")",
                'btnStyle' => 'info',
                'icon' => 'fa fa-ok'
            ];
        }
        $btn['删除'] = [
            'auth' => 'complaint/del',
            'href' => "javascript:dataDel(" .$id .")",
            'btnStyle' => 'danger',
            'icon' => 'fa fa-trash-o'
        ];
        return $btn;
    }
}
